<template>
  <div class="app-container">

    <!-- 添加或修改车辆维修与保养对话框 -->
    <el-dialog title="维修保养" :visible="openWx" width="1000px" append-to-body @close="cancel">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" inline>
        <el-row style="padding-left: 120px">
          <el-form-item >
            <div style="color: red">维修保养暂不影响库存，请您及时下线车辆</div>
          </el-form-item>
        </el-row>

        <el-form-item label="车牌号" prop="carPlateNo">
          <el-input v-model="form.carPlateNo" placeholder="请输入车牌号" readonly style="width: 300px"/>
        </el-form-item>
        <el-form-item label="维保类型">
          <el-radio-group v-model="form.type">
            <el-radio
              v-for="dict in dict.type.repair_type"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="送车时间" prop="repairStartDate">
          <el-date-picker clearable size="small"
                          style="width: 300px"
            v-model="form.repairStartDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择送车时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="当前里程" prop="carPlateNo">
          <el-input v-model="form.useMileage" placeholder="请输入当前里程"  style="width: 300px"/>
        </el-form-item>
        <el-form-item label="维修厂" prop="serviceId">
          <el-select v-model="form.serviceId" placeholder="请选择维修厂" class="el-input-style" @change="checkSupplier"   filterable remote   style="width: 300px">
            <el-option v-for="item in supplierList" :key="item.id" :label="item.name" :value="parseInt(item.id)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关联订单" prop="orderNo">
          <el-select v-model="form.orderNo" placeholder="请选择关联订单" filterable  class="el-input-style"  style="width: 300px">
            <el-option v-for="item in orderList" :key="item.orderNo" :label="item.orderNo" :value="item.orderNo"></el-option>
          </el-select>
        </el-form-item>
        <el-row>
          <el-form-item label="维保项目" prop="remark">
            <el-input
              type="textarea"
              :rows="2"
              style="width: 730px"
              maxlength="200"
              show-word-limit
              placeholder="维保项目"
              v-model="form.maintainRemark">
            </el-input>
          </el-form-item>
        </el-row>
        <el-form-item label="维保附件" prop="upcoverImage">
          <imageUpload isArray v-model="form.imgList" ref="imageUpload" v-bind:limit="5"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="loading" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addRepairMaintain,getSupplierList } from "@/api/car/repairMaintain";
import {orderList} from '@/api/order/index'
export default {
  name: "RepairMaintain",
  dicts: ['repair_status', 'repair_type'],
  props: {
    carId: {
      type: String,
      default: ''
    },
    carPlateNo: {
      type: String,
      default: ''
    },
    useMileage: {
      type: Number,
      default: 0
    },
    openWx: {
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      supplierList:[],
      // 遮罩层
      loading: false,
      // 弹出层标题
      title: "",
      orderList:[],
      // 表单参数
      form: {
        type:1,
        carId: this.carId,
        carPlateNo: this.carPlateNo,
        useMileage: this.useMileage,
        repairStatus: null,
        totalPay: null,
        repairStartDate: null,
        repairEndDate: null,
        serviceId: null,
        serviceName: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        imgList: [],
        tenantId: null
      },
      // 表单校验
      rules: {
        carPlateNo: [{
          required: true,
          message: '车牌号不能为空',
          trigger: 'blue'
        }],
        type: [{
          required: true,
          message: '维修类型不能为空',
          trigger: 'blue'
        }],
        repairStartDate: [{
          required: true,
          message: '送车时间不能为空',
          trigger: 'blue'
        }],
        serviceId: [{
          required: true,
          message: '维修厂不能为空',
          trigger: 'blue'
        }],
      }
    };
  },
  created() {
    getSupplierList(2).then(response => {
      this.supplierList = response.data;
    });
    orderList({"carId":this.carId,"status":"0,10",keyword:""}).then(response => {
      this.orderList=response.data;
    });
  },
  methods: {

    // 添加实物图片
    uppeerImage(url) {
      // 分享封面上传成功地址
      this.form.imgList = url
    },
    // 取消按钮
    cancel() {
      this.reset();
      this.$emit("close")
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        carId: null,
        carPlateNo: null,
        repairStatus: null,
        totalPay: null,
        repairStartDate: null,
        repairEndDate: null,
        type: 1,
        serviceId: null,
        serviceName: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        imgList: [],
        tenantId: null
      };
      this.resetForm("form");
    },
    checkSupplier(){
      this.form.serviceName=this.supplierList.filter(item=>{
         if(item.id==this.form.serviceId){
           return item.name
         }
      })[0].name
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          addRepairMaintain(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.cancel()
          });
        }
      });
      setTimeout(()=> {
        this.loading = false;
      },2e3)
    }
  }
};
</script>
